package com.aletto.general_store.ui.activities.orders;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.OrderCompany;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.utils.XHelper;
import com.lxj.xpopup.XPopup;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.ToastView;
import com.sisyphus.spinach.utils.watchers.EmptyRule;
import com.sisyphus.spinach.utils.watchers.TextWatchers;

/**
 * 发货
 */
public class OrderFormActivity extends AppCompatActivity {

    public static Intent newIntent(Context context, Long orderId) {
        Intent intent = new Intent(context, OrderFormActivity.class);
        intent.putExtra("ORDER_ID", orderId);
        return intent;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_form);

        Button buttonConfirm = findViewById(R.id.button_confirm);
        EditText editOrderNumber = findViewById(R.id.edit_order_number);
        TextView editOrderCompany = findViewById(R.id.edit_order_company);

        // 标题
        XHelper.initTitle(this, R.string.title_order_form);

        buttonConfirm.setOnClickListener(v ->
                task(
                        getIntent().getLongExtra("ORDER_ID", 0),
                        editOrderNumber.getText(),
                        String.valueOf(((OrderCompany) editOrderCompany.getTag()).getId())
                ).execute(getLifecycle())
        );

        // 快递公司弹窗
        OrderCompanyPopup orderCompanyPopup = new OrderCompanyPopup(this, item -> {
            editOrderCompany.setTag(item);
            editOrderCompany.setText(item.getName());
        });

        // 选择快递公司
        editOrderCompany.setOnClickListener(v -> new XPopup.Builder(v.getContext()).asCustom(orderCompanyPopup).show());

        // 可点击响应
        TextWatchers.add(new EmptyRule(editOrderNumber, editOrderCompany)).setWatcherCallback(buttonConfirm::setEnabled);
    }


    private SimpleTemplate<Object> task(Long orderId, CharSequence sendId, CharSequence sendCode) {
        return new SimpleTemplate<>() {

            @Override
            public INetTask.View<Object> view() {
                return new ToastView<>(getApplication()){
                    @Override
                    public void success(Object data) {
                        finish();
                    }
                };
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().sendOrder(orderId, sendId.toString(), sendCode.toString()));
            }
        };
    }

}
